/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.poemmanager.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.poemPoet.domain.PoemPoetFavorites;
import me.zhengjie.poemPoet.service.PoemPoetFavoritesService;
import me.zhengjie.poemUser.domain.PoemUserFavorites;
import me.zhengjie.poemUser.service.PoemUserFavoritesService;
import me.zhengjie.poemmanager.domain.Favorites;
import me.zhengjie.poemmanager.domain.vo.FavoritesQueryCriteria;
import me.zhengjie.poemmanager.service.FavoritesService;
import me.zhengjie.post.domain.PostFavorites;
import me.zhengjie.post.service.PostFavoritesService;
import me.zhengjie.recitation.domain.RecitationFavorites;
import me.zhengjie.recitation.service.RecitationFavoritesService;
import me.zhengjie.utils.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author hwl
* @date 2024-12-07
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "favorites管理")
@RequestMapping("/api/favorites")
public class FavoritesController {

    private final FavoritesService favoritesService;
    private final PoemUserFavoritesService poemUserFavoritesService;
    private final PoemPoetFavoritesService poemPoetFavoritesService;
    private final PostFavoritesService postFavoritesService;
    private final RecitationFavoritesService recitationFavoritesService;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @AnonymousAccess
    public void exportFavorites(HttpServletResponse response, FavoritesQueryCriteria criteria) throws IOException {
        favoritesService.download(favoritesService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询favorites")
    @ApiOperation("查询favorites")
    @AnonymousAccess
    public ResponseEntity<PageResult<Favorites>> queryFavorites(FavoritesQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(favoritesService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping(value = "/paged")
    @Log("分页查询favorites")
    @ApiOperation("分页查询favorites")
    @AnonymousAccess
    public ResponseEntity<PageResult<Favorites>> pagedQueryFavorites(FavoritesQueryCriteria criteria,
                                                                     @RequestParam(defaultValue = "1") int current,
                                                                     @RequestParam(defaultValue = "20") int size  ){
        Page<Object> page = new Page<>(current,size);
        return new ResponseEntity<>(favoritesService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增favorites")
    @ApiOperation("新增favorites")
    @AnonymousAccess
    public ResponseEntity<Object> createFavorites(@Validated @RequestBody Favorites resources){
    //    favoritesService.create(resources);
        switch (resources.getObjectType()) {
            case 0:
                postFavoritesService.create(convertToPostFavorites(resources));
                break;
            case 1:
                poemUserFavoritesService.create(convertToPoemUserFavorites(resources));
                break;
            case 2:
                poemPoetFavoritesService.create(convertToPoemPoetFavorites(resources));
                break;
            case 3:
                recitationFavoritesService.create(convertToRecitationFavorites(resources));
                break;
            default:
                throw new IllegalArgumentException("无效的收藏类型: " + resources.getObjectType());
        }
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改favorites")
    @ApiOperation("修改favorites")
    @AnonymousAccess
    public ResponseEntity<Object> updateFavorites(@Validated @RequestBody Favorites resources){
        favoritesService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除favorites")
    @ApiOperation("删除favorites")
    @AnonymousAccess
    public ResponseEntity<Object> deleteFavorites(@RequestParam Integer type,@RequestBody List<Integer> ids) {
    //    favoritesService.deleteAll(ids);
        switch(type)
        {
            case 0:
                postFavoritesService.deleteAll(ids);
                break;
            case 1:
                poemUserFavoritesService.deleteAll(ids);
                break;
            case 2:
                poemPoetFavoritesService.deleteAll(ids);
                break;
            case 3:
                recitationFavoritesService.deleteAll(ids);
                break;
            default:
                throw new IllegalArgumentException("无效的收藏类型: " + type);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("isBeFavourited")
    @Log("判断该帖子是否被本用户关注")
    @ApiOperation("判断favorites")
    @AnonymousAccess
    public Favorites isBeFavourite(@RequestParam Integer objectId,@RequestParam Integer objectType,@RequestParam String userName) {
        switch(objectType)
        {
            case 0:
                return postFavoritesService.isBeFavourite(objectId,userName);
            case 1:
                return poemUserFavoritesService.isBeFavourite(objectId,userName);
            case 2:
                return poemPoetFavoritesService.isBeFavourite(objectId,userName);
            case 3:
                return recitationFavoritesService.isBeFavourite(objectId,userName);
            default:
                throw new IllegalArgumentException("无效的收藏类型: " + objectType);
        }
    }

    // 转换方法组（返回具体子类）
    private PostFavorites convertToPostFavorites(Favorites origin) {
        PostFavorites target = new PostFavorites();
        BeanUtils.copyProperties(origin, target, "objectType");
        return target;
    }

    private PoemUserFavorites convertToPoemUserFavorites(Favorites origin) {
        PoemUserFavorites target = new PoemUserFavorites();
        BeanUtils.copyProperties(origin, target, "objectType");
        return target;
    }

    private PoemPoetFavorites convertToPoemPoetFavorites(Favorites origin) {
        PoemPoetFavorites target = new PoemPoetFavorites();
        BeanUtils.copyProperties(origin, target, "objectType");
        return target;
    }

    private RecitationFavorites convertToRecitationFavorites(Favorites origin) {
        RecitationFavorites target = new RecitationFavorites();
        BeanUtils.copyProperties(origin, target, "objectType");
        return target;
    }

}